package UIComponent
{
	import flash.display.Graphics;
	import flash.events.MouseEvent;
	
	import mx.controls.Button;
	import mx.controls.Label;
	import mx.core.UIComponent;
	import mx.effects.effectClasses.PropertyChanges;
	import mx.events.PropertyChangeEvent;
	
	import sprites.DataSourceSprite;

	public class PointSprite extends UIComponent
	{	
		//essential
		private var skeleton:PointSkeleton;
		public var canvas:MyCanvas;
		
		//component
		private var label:DynamicLabel;
		private var convert:Button;
		private var selectThis:Button;
		private var removeThis:Button;
		private var discription:DynamicLabel;
		
		//status
		protected var selected:Boolean;
		protected var expand:Boolean;
		private var highlight:Number=0;
		private var color:uint= 0x707D7E;
		
		//static
		protected var DEFAULT_MINI_HEIGHT:Number=24;
		protected var DEFAULT_MINI_WIDTH:Number=250;
		protected var DEFAULT_MAXI_HEIGHT:Number=200;
		protected var MARGIN_WIDTH:Number=2;
		protected var MARGIN_HEIGHT:Number=2;
		protected var CONVERT_WIDTH:Number=25;
		protected var SELECT_WIDTH:Number=25;
		protected var REMOVE_WIDTH:Number=25;
		protected var BUTTON_WIDTH:Number=25;
		protected var HIGHLIGHT_MARGIN:Number=2;
		protected var CASE_COLOR:uint = 0x01A9A8;
		protected var DEFAULT_COLOR:uint = 0x028685;
		protected var ZOMBIE_COLOR:uint = 0x707D7E;
		protected var POISON_COLOR:uint = 0x516402;
		
		//life
		private var lifeStatus:Number=0;		//0-Zombie 1-Living 2-Poisoned
		private var lifeValue:Number=0;
		private var poisonValue:Number=0;
		
		public function PointSprite(_pointSkeleton:PointSkeleton, _canvas:MyCanvas)
		{
			//set essential
			skeleton=_pointSkeleton;
			canvas=_canvas;
			//this.doubleClickEnabled=true;
			
			//set status
			selected=false;
			expand=false;
			
			//add select this button
			selectThis=new Button();
			
			this.addChild(selectThis);
			selectThis.label="o";
			selectThis.x=MARGIN_WIDTH;
			selectThis.y=MARGIN_HEIGHT;
			selectThis.width=SELECT_WIDTH;
			selectThis.height=DEFAULT_MINI_HEIGHT - MARGIN_HEIGHT*2;
			selectThis.addEventListener(MouseEvent.CLICK,onSelected);
				
			//add label
			label=new DynamicLabel();
			this.addChild(label);
			label.setText(skeleton.getName());
			label.x=MARGIN_WIDTH*2+SELECT_WIDTH;
			label.y=MARGIN_HEIGHT;
			label.setEventListener(PropertyChangeEvent.PROPERTY_CHANGE,onChanged);
			label.showBackground(true);
					
			//add remvoe button
			removeThis=new Button();
			this.addChild(removeThis);
			removeThis.label="x";
			removeThis.x=DEFAULT_MINI_WIDTH-CONVERT_WIDTH-MARGIN_WIDTH*2-REMOVE_WIDTH;
			removeThis.y=MARGIN_HEIGHT;
			removeThis.width=REMOVE_WIDTH;
			removeThis.height=DEFAULT_MINI_HEIGHT - MARGIN_HEIGHT*2;
			//removeThis.setImage("../rec/remove_n.png","../rec/remove_o.png","../rec/remove_o.png");
			removeThis.addEventListener(MouseEvent.CLICK,onRemove);
			
			//add convert button
			convert=new Button();
			this.addChild(convert);
			convert.label="v";
			convert.x=DEFAULT_MINI_WIDTH-CONVERT_WIDTH-MARGIN_WIDTH;
			convert.y=MARGIN_HEIGHT;
			convert.width=CONVERT_WIDTH;
			convert.height=DEFAULT_MINI_HEIGHT - MARGIN_HEIGHT*2;
			convert.addEventListener(MouseEvent.CLICK,onConvert);
			
		}
		
		//when property of label changed
	    protected function onChanged(evt:PropertyChangeEvent):void
		{		
		}
		//when click selected button
		protected function onSelected(evt:MouseEvent):void
		{
			if(selected==false)
			{
				selected=true;
			}
			else
			{
				selected=false;
			}
			this.invalidateDisplayList();
		}
		
		//draw me
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			var g2d:Graphics=this.graphics;
			g2d.clear();
			
			if(highlight>0)
			{
				g2d.beginFill(0xFF8888);
				g2d.drawRoundRect(-1*HIGHLIGHT_MARGIN,-1*HIGHLIGHT_MARGIN, getWidth()+HIGHLIGHT_MARGIN*2, getHeight()+HIGHLIGHT_MARGIN*2,4);
				g2d.endFill();
			}
						
			var curColor:uint=color;
			if(lifeStatus == 1)
			{
				curColor=DEFAULT_COLOR;
			}
			else if(lifeStatus ==2)
			{
				curColor=POISON_COLOR;
			}
			g2d.beginFill(curColor,1.0);
			
			g2d.drawRoundRect(0,0,this.getWidth() ,this.getHeight(),3,3);
			g2d.endFill();
			
			super.updateDisplayList(unscaledWidth,unscaledHeight);
		}
		
		//when click convert button
		protected function onConvert(evt:MouseEvent):void
		{
			convertExpand();
		}
		
		//when click remove button
		protected function onRemove(evt:MouseEvent):void
		{
			removeThis.mouseEnabled=false;
			removeThis.invalidateDisplayList();
		}
		
		//convert between mini and maxi
		protected function convertExpand():void
		{
			if(expand==true)
			{
				expand=false;
				this.removeChild(discription);
				discription=null;
			}
			else
			{
				expand=true;
				discription=new DynamicLabel(1,DEFAULT_MINI_WIDTH - MARGIN_WIDTH*2,DEFAULT_MAXI_HEIGHT - MARGIN_HEIGHT*2 - DEFAULT_MINI_HEIGHT);
				this.addChild(discription);
				discription.setText(skeleton.getDiscription());
				discription.x=MARGIN_WIDTH;
				discription.y=MARGIN_HEIGHT + DEFAULT_MINI_HEIGHT;
				discription.showBackground(true);
				discription.setEventListener(PropertyChangeEvent.PROPERTY_CHANGE,onDiscriptionChanged);
				
			}
			this.invalidateDisplayList();
			this.canvas.invalidateDisplayList();
		}
		
		protected function onDiscriptionChanged(evt:PropertyChangeEvent):void
		{
		}
		
		//get width of this component
		public function getWidth():int
		{
			return DEFAULT_MINI_WIDTH;
		}
		
		//get height of this component
		public function getHeight():int
		{
			if(expand==false)
			{
				return DEFAULT_MINI_HEIGHT;
			}
			else
			{
				return DEFAULT_MAXI_HEIGHT;
			}
		}
		
		//when mouse moves in
		protected function onMouseIn(evt:MouseEvent):void
		{
			removeThis.visible=true;
			convert.visible=true;
		}
		
		//when mouse moves out
		protected function onMouseOut(evt:MouseEvent):void
		{
			removeThis.visible=false;
			convert.visible=false;
		}
		
		public function remove():void
		{
		}
		
		public function getSkeleton():PointSkeleton
		{
			return skeleton;
		}
		
		public function setSkeleton(_skeleton:PointSkeleton):void
		{
			skeleton=_skeleton;
		}

		public function getHighlight():Boolean
		{
			return (highlight!=0);
		}

		public function setHighlight(value:Boolean):void
		{
			if(value==true)
			{
				highlight++;
			}
			else
			{
				highlight--;
				if(highlight<0)
				{
					highlight=0;
				}
			}
		}
		
		public function increaseLife():void
		{
			lifeValue++;
			//status judge
			setStatus();	
		}
		
		public function decreaseLife():void
		{
			lifeValue--;
			if(lifeValue<0)
			{
				lifeValue=0;
			}
			//status judge
			setStatus();
		}
		
		public function increasePoison():void
		{
			poisonValue++;
			//status judge
			setStatus();
		}
		
		public function decreasePoison():void
		{
			poisonValue--;
			if(poisonValue<0)
			{
				poisonValue==0;
				//status judge
			}
			setStatus();
		}
		
		private function setStatus():void
		{
			var oldStatus:Number=lifeStatus;
			if(poisonValue>0)
			{
				lifeStatus=2;
			}
			else if(lifeValue>0)
			{
				lifeStatus=1;
			}
			else
			{
				lifeStatus=0;
			}
			
			//do life state change dispatch
			if(oldStatus == 1 && lifeStatus !=1) //turn without life
			{
				turnNoLife();		//do action
			}
			else if( oldStatus !=1 && lifeStatus==1) //turn to life
			{
				turnLife();		//do action
			}
			
			if(oldStatus!=lifeStatus)
			{
				this.invalidateDisplayList();
			}
		}
		
		protected function turnNoLife():void
		{		
		}
		
		protected function turnLife():void
		{	
		}
		
		public function getLifeStatus():Number
		{
			return lifeStatus;
		}

		public function getColor():uint
		{
			return color;
		}

		public function setColor(value:uint):void
		{
			color = value;
		}
		
		public function colorAfter():void
		{
			
		}
		
		public function highlightAfter(_showDelButton:Boolean):void
		{
		}
		
		public function highlightBefore(_showDelButton:Boolean):void
		{
		
		}
		
		public function resetHighlight():void
		{
			highlight=0;
		}
		
		public function resetColor():void
		{
			lifeValue=0;
			poisonValue=0;
			lifeStatus = 0;
		}

		public function onSetTrace():void
		{
			if(canvas.getTrackingObject() ==null )
			{
				canvas.setTrackingObject(this);
			}
			else
			{
				if(canvas.getTrackingObject() == this)
				{
					canvas.setTrackingObject(null);
				}
				else
				{
					canvas.setTrackingObject(this);
				}
			}
		}
		
		public function removeButRecover():void
		{
			removeThis.enabled =true;
		}
		
		public function showFromDelButton(_relation:LineSkeleton):void
		{
		}
		
		public function showDeriveDelButton(_relation:LineSkeleton):void
		{
		}
		
		
			
	}
}